/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.binding.guicomponents.action;

import java.util.*;
import javax.swing.*;
import org.jdesktop.observablecollections.*;
import ch.trackedbean.binding.mapping.*;

/**
 * Action for {@link ObservableList} dependent stuff.<br>
 * Use this action if your action is dependent of a list state.
 * 
 * @author M. Hautle
 */
public abstract class AbstractListDependentAction extends AbstractAction implements IDependentElement, ObservableListListener {
	/** The observed list or null. */
	protected ObservableList list;

	/**
	 * Default constructor.
	 */
	public AbstractListDependentAction() {
	}

	/**
	 * Default constructor.
	 * 
	 * @param name The action name
	 * @param icon The action icon
	 */
	public AbstractListDependentAction(String name, Icon icon) {
		super(name, icon);
	}

	/**
	 * Default constructor.
	 * 
	 * @param name The action name
	 */
	public AbstractListDependentAction(String name) {
		super(name);
	}

	/**
	 * Returns the list.
	 * 
	 * @return The list or null
	 */
	public List getList() {
		return list;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setDependentValue(Object value) {
		if (!(value == null || value instanceof ObservableList))
			throw new IllegalArgumentException("Only instances of " + ObservableList.class.getName() + " are supported!");
		if (list != null)
			list.removeObservableListListener(this);
		list = (ObservableList) value;
		if (list != null)
			list.addObservableListListener(this);
		listChanged();
	}

	/**
	 * Method called if the observed list changes (if it was changed or if an internal change occourd).<br>
	 * The return value of this method is used to enable/disable the action.
	 * 
	 * @param list The list (same as {@link #list})
	 * @return True if the action has to be enabled
	 */
	public abstract boolean listChanged(List list);

	/**
	 * The list changed.
	 */
	private void listChanged() {
		setEnabled(listChanged(list));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void listElementPropertyChanged(ObservableList list, int index) {
		listChanged();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void listElementReplaced(ObservableList list, int index, Object oldElement) {
		listChanged();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void listElementsAdded(ObservableList list, int index, int length) {
		listChanged();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void listElementsRemoved(ObservableList list, int index, List oldElements) {
		listChanged();
	}
}
